package modelLayer;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


public class SalesOrder {
	private int orderId;
	private String orderDate;
	private boolean deliveryStatus; //False: Not delivered. True: Delivered.
	private String deliveryDate;
	private Customer customer;
	private int invoiceNo;
	private List<SalesLine> salesLines;

	/**
	 * Constructor that constructs the object before it is inserted into the database.
	 * 
	 * @param customer
	 */
	public SalesOrder(Customer customer) {
		this.customer = customer;
		this.deliveryStatus = false;
		this.invoiceNo = 0;
		salesLines = new ArrayList<SalesLine>();
	}

	/**
	 * Constructor that constructs the object when data is retrieved from the database.
	 */
	public SalesOrder() {
		salesLines = new ArrayList<SalesLine>();
	}

	/**
	 * Method for adding a product to the order.
	 * A salesLine object is created, and stored in the list of salesLines.
	 * 
	 * @param p 		The product that is to be added.
	 * @param quantity 	The quantity of the product.
	 * @return			The salesLine object that is created, so it can be stored in the database.
	 */
	public SalesLine addProduct(Product p, int quantity) {
		SalesLine sl = new SalesLine(p, quantity);
		boolean found = false;
		for(SalesLine sline: salesLines){
			if(sline.getProduct().getProductId() == p.getProductId()){
				found = true;
				sline.setQuantity(quantity);
			}
		}
		if(!found){
			this.salesLines.add(sl);
		}
		return sl;
	}
	/**
	 * Method that calculates and returns the total amount on the order.
	 * 
	 * @return			The calculated amount.
	 */
	public double calculateOrderAmount() {
		double amount = 0.0;
		for (SalesLine sl : salesLines) {
			amount += sl.calculateLineAmount();
		}
		return amount;
	}

	/**
	 * @return the orderId
	 */
	public int getOrderId() {
		return orderId;
	}

	/**
	 * @param orderId the orderId to set
	 */
	public void setOrderId(int orderId) {
		this.orderId = orderId;
	}

	/**
	 * @return the orderDate
	 */
	public String getOrderDate() {
		return orderDate;
	}

	/**
	 * @param orderDate the orderDate to set
	 */
	public void setOrderDate(String orderDate) {
		this.orderDate = orderDate;
	}

	/**
	 * @return the deliveryStatus
	 */
	public boolean isDeliveryStatus() {
		return deliveryStatus;
	}

	/**
	 * @param deliveryStatus the deliveryStatus to set
	 */
	public void setDeliveryStatus(boolean deliveryStatus) {
		this.deliveryStatus = deliveryStatus;
	}

	/**
	 * @return the deliveryDate
	 */
	public String getDeliveryDate() {
		return deliveryDate;
	}

	/**
	 * @param deliveryDate the deliveryDate to set
	 */
	public void setDeliveryDate(String deliveryDate) {
		this.deliveryDate = deliveryDate;
	}

	/**
	 * @return the customer
	 */
	public Customer getCustomer() {
		return customer;
	}

	/**
	 * @param customer the customer to set
	 */
	public void setCustomer(Customer customer) {
		this.customer = customer;
	}

	/**
	 * @return the salesLines
	 */
	public List<SalesLine> getSalesLines() {
		return salesLines;
	}

	/**
	 * @param salesLines the salesLines to set
	 */
	public void setSalesLines(List<SalesLine> salesLines) {
		this.salesLines = salesLines;
	}

	/**
	 * @return the invoiceNo
	 */
	public int getInvoiceNo() {
		return invoiceNo;
	}

	/**
	 * @param invoiceNo the invoiceNo to set
	 */
	public void setInvoiceNo(int invoiceNo) {
		this.invoiceNo = invoiceNo;
	}
}
